package com.example.weathermvvm.ui.area

import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.weathermvvm.MyOwnApplication
import com.example.weathermvvm.data.PlaceRepository
import com.example.weathermvvm.data.model.place.City
import com.example.weathermvvm.data.model.place.County
import com.example.weathermvvm.data.model.place.Province
import kotlinx.coroutines.launch
import java.util.ArrayList

/**
 *
 * @Author： LJH
 * @Time： 2022/8/10
 * @description：
 */
class ChooseAreaViewModel(private val placeRepository: PlaceRepository):ViewModel() {

    //是否加载标识
    var isLoading = MutableLiveData<Boolean>()

    //当前的地区标识
    var currentLevel = MutableLiveData<Int>()

    //true 代表已选择省份
    var areaSelect = MutableLiveData<Boolean>()

    //数据刷新标识
    var dataChanged = MutableLiveData<Int>()

    //省份数据集合
    lateinit var provinces:MutableList<Province>

    //城市数据集合
    lateinit var cities:MutableList<City>
    //县城数据集合
    lateinit var counties:MutableList<County>

    //当前显示的数据集合
    val dataList = ArrayList<String>()

    //  已选择的省份
    var selectedProvince:Province? = null
    //  已选择的城市
    var selectedCity:City? = null
    //  已选择的县城
    var selectedCounty:County? = null

    /**
     * 获取省份名称列表
     */
    fun getProvinces(){
        Log.e("viewModel","获取省份名称列表。。。")
        currentLevel.value = ChooseAreaFragment.LEVEL_PROVINCE
        launch {
            provinces = placeRepository.getProvinces()
            dataList.addAll(provinces.map { it.provinceName })
        }
        dataList.forEach {
            Log.e("viewModel","$it")
        }
    }

    fun onListViewItemClick(parent:AdapterView<*>,view:View,position:Int,id:Long){
        when(currentLevel.value){
            ChooseAreaFragment.LEVEL_PROVINCE -> {
                selectedProvince = provinces[position]
                getCities()
            }
            ChooseAreaFragment.LEVEL_CITY -> {
                selectedCity = cities[position]
                getCounties()
            }
            ChooseAreaFragment.LEVEL_COUNTY -> {
                selectedCounty = counties[position]
                areaSelect.value = true
            }
        }
    }

    private fun getCities() = selectedProvince?.let {
        currentLevel.value = ChooseAreaFragment.LEVEL_CITY
        launch {
            cities = placeRepository.getCities(it.provinceCode)
            dataList.addAll(cities.map { it.cityName })
        }
    }

    private fun getCounties() = selectedCity?.let {
        currentLevel.value = ChooseAreaFragment.LEVEL_COUNTY
        launch {
            counties = placeRepository.getCounties(it.provinceId,it.cityCode)
            dataList.addAll(counties.map { it.countyName })
        }
    }

    fun onBack(){
        if(currentLevel.value == ChooseAreaFragment.LEVEL_COUNTY){
            getCities()
        }else if (currentLevel.value == ChooseAreaFragment.LEVEL_CITY){
            getProvinces()
        }
    }

    private fun launch(block:suspend () -> Unit) = viewModelScope.launch {
        try {
            isLoading.value = true
            dataList.clear()
            block()
            dataChanged.value = dataChanged.value?.plus(1)
            isLoading.value = false
        }catch (t:Throwable){
            t.printStackTrace()
            Toast.makeText(MyOwnApplication.context,t.message,Toast.LENGTH_SHORT).show()
            dataChanged.value = dataChanged.value?.plus(1)
            isLoading.value = false
        }
    }
}